﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Elifart.Library;
using Elifart.Editor.GeneralControls;

namespace Elifart.Editor.CategoryTabs.TeamControls
{
    public partial class TeamControl : UserControl
    {
        #region Constructors

        public TeamControl()
        {
            InitializeComponent();
        }

        #endregion

        #region Private Methods

        private void LoadTeam(Team team)
        {
            this.LoadInfo(team);
            this.LoadAthletes(team);

            this.pnlTeamInfo.Enabled = this.lbxAthlethes.Enabled = true;
        }

        private void LoadInfo(Team team)
        {
            this.txtTeamName.Text = team.Name;
            this.dtpFoundationDate.Value = team.FoundingDate;
        }

        private void LoadAthletes(Team team)
        {
            this.lbxAthlethes.DataSource = team.Athletes;
        }

        private void ResetTeam()
        {
            this.txtTeamName.Text = string.Empty;
            this.dtpFoundationDate.Value = DateTime.Now;
            this.lbxAthlethes.DataSource = null;
        }

        #endregion

        #region Signed Events Methods

        private void btnNewTeam_Click(object sender, EventArgs e)
        {
            Team newTeam = new Team();

            TeamManager.GetManager().Teams.Add(newTeam);

            this.lbxTeams.Items.Add(newTeam);
            this.lbxTeams.SelectedItem = newTeam;
        }

        private void lbxTeams_SelectedIndexChanged(object sender, EventArgs e)
        {
            Team selectedTeam = this.lbxTeams.SelectedItem as Team;

            if (selectedTeam != null)
                this.LoadTeam(this.lbxTeams.SelectedItem as Team);
            else
                this.ResetTeam();

            this.btnManageAthletes.Enabled = selectedTeam != null;
            this.btnRemoveTeam.Enabled = selectedTeam != null;
            this.pnlTeamInfo.Enabled = this.lbxAthlethes.Enabled = selectedTeam != null;
        }

        private void btnManageAthletes_Click(object sender, EventArgs e)
        {
            if (this.lbxTeams.SelectedItem == null)
                return;

            Team team = this.lbxTeams.SelectedItem as Team;
            if (team == null)
                return;

            ChooseAthletesDialog dialog = new ChooseAthletesDialog(AthleteManager.GetManager().Athletes, team.Athletes);
            dialog.ShowDialog();
        }

        private void btnRemoveTeam_Click(object sender, EventArgs e)
        {
            if (this.lbxTeams.SelectedItem == null)
                return;

            Team team = this.lbxTeams.SelectedItem as Team;
            if (team == null)
                return;

            TeamManager.GetManager().Teams.Remove(team);

            this.lbxTeams.Items.Remove(team);
        }

        #endregion
    }
}
